﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ZedGraph;


namespace Plotter
{
    public class Plotter: IPlotter
    {
        PlotData data;
        const int numInterval = 20;

        public Plotter(PlotData d)
        {
            this.data = d;
        }

        public void OnRenderGraph(ZedGraph.Web.ZedGraphWeb z, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
        {
            // Get the GraphPane so we can work with it
            GraphPane myPane = masterPane[0];

            // Set the titles and axis labels
            myPane.Title.Text = "Статистика";
            myPane.XAxis.Title.Text = "Время";
            myPane.YAxis.Title.Text = data.axisName;

            PointPairList list;
            ColorSymbolRotator rotator = new ColorSymbolRotator();
            DateTime maxTime, minTime;
            maxTime = DateTime.MinValue;
            minTime = DateTime.MaxValue;
            
            foreach ( Curve c in data.curves )
            {
                if ( DateTime.Compare( maxTime, c.points[c.points.Length - 1].time ) < 0 )
                    maxTime = c.points[c.points.Length - 1].time;
                if ( DateTime.Compare( minTime, c.points[0].time ) > 0 )
                    minTime = c.points[0].time;
            }
            double maxT = new XDate(maxTime);
            double minT = new XDate( minTime );
            double unit = (maxT-minT)/numInterval;

            foreach (Curve c in data.curves)
            {
                list = new PointPairList();
                double sy = 0;
                int n = 0;
                double interval = unit;
                for ( int id = 0; id < c.points.Length; id++ )
                {
                    double x = new XDate( c.points[id].time );
                    double y = c.points[id].value;
                    if ( x < interval + minT )
                    {
                        sy += y;
                        n++;
                    }
                    else
                    {
                        if ( n != 0 )
                            list.Add( minT + interval - unit / 2, sy / n );
                        else
                            list.Add( minT + interval - unit / 2, sy );
                        interval += unit;
                        sy = 0;
                        n = 0;
                        id--;
                    }
                }          

                // Generate a red curve with diamond symbols, and "My Curve" in the legend
                LineItem myCurve = myPane.AddCurve(c.name,
                    list, rotator.NextColor, rotator.NextSymbol); //Color.MediumVioletRed, SymbolType.Circle);
               
                // Make the symbols opaque by filling them with white
                myCurve.Symbol.Fill = new Fill(Color.Black);
               
                // Set the XAxis to date type
                myPane.XAxis.Type = AxisType.Date;
                myPane.XAxis.CrossAuto = true;

                // Fill the axis background with a color gradient
                myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

                masterPane.AxisChange(g);

            }
        }
    }
}
